<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserExperienceModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserIdentityCardModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\OpenShop\StartBusiness\OpenShopStartBusinessModel;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class CruiseShopAccountModel extends BaseModel
{
    protected $table = 'cruise_shop_account';

    protected $fillable = [
        'shopId',
        'turnover',
        'orderAmount',
        'createdAt',
        'updatedAt',
        'orderNo',
        'area',
        'managerName',
        'preOpenTime',
        'provinceCode',
        'cityCode',
        'districtCode',
        'franchiseeName',
        'shopName',
        'shopCode',
        'businessStatus',
        'orderRatioNumber',
        'daysWithoutCruise',
    ];

    protected $appends = [
        'orderRatio'
    ];

    const GRADE_QUESTION   = 1;
    const GRADE_SKINNY_DOG = 2;
    const GRADE_TAURUS     = 3;
    const GRADE_STAR       = 4;

    //用于筛选
    const SHOP_GRADE_MAPPING = [
        self::GRADE_QUESTION   => 'ST',
        self::GRADE_SKINNY_DOG => '瘦狗',
        self::GRADE_TAURUS     => '金牛',
        self::GRADE_STAR       => '明星',
    ];

    /**
     * 开业状态
     */
    const BUSINESS_STATUS_ON      = 1;
    const BUSINESS_STATUS_OFF     = 0;
    const BUSINESS_STATUS_MAPPING = [
        self::BUSINESS_STATUS_ON  => '已开业',
        self::BUSINESS_STATUS_OFF => '未开业',
    ];


    public function getOrderRatioAttribute()
    {
        if ($this->turnover == 0 || $this->orderAmount == 0) {
            return '-';
        }
        return (round($this->orderAmount / $this->turnover, 2) * 100) . '%';
    }

    /**
     * @return HasManyThrough
     */
    public function orderManagerRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopOrderManagerModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id');
    }



    /**
     * @return HasManyThrough
     */
    public function userIdentityCardRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopUserIdentityCardModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'orderNo',
            'shopId',
            'orderNo');
    }


    /**
     * @return HasManyThrough
     */
    public function siteLocationRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopSiteLocationModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id')->whereIn('source',
            [OpenShopSiteLocationModel::SOURCE_OPEN_SHOP, OpenShopSiteLocationModel::SOURCE_HOUSING]);
    }


    /**
     * @return HasManyThrough
     */
    public function franchiseeArchivesRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            FranchiseeArchiveModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id');
    }

    /**
     * @return HasManyThrough
     */
    public function userWorkExperienceRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopUserExperienceModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'orderNo',
            'shopId',
            'orderNo');
    }


    /**
     * @return HasManyThrough
     */
    public function startBusinessRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopStartBusinessModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id');
    }



    /**
     * @return HasManyThrough
     */
    public function signOrderRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopSignOrderModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id');
    }

    /**
     * @return HasMany
     */
    public function cruiseOrderRel(): HasMany
    {
        return $this->hasMany(CruiseOrderModel::class, 'shopId', 'shopId');
    }


}
